package me.xuym.vim.mddirectory;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by broche on 3/10/16.
 */
public class Main {
    private static final boolean isDebug = false;
    private static final String directoryStart = "<a name='dstart'></a>";
    private static final String directoryEnd = "<a name='dend'></a>";
    private static final Pattern sectionTitlePattern = Pattern.compile("((#+)[ \t]+(.*))");
    private static final Pattern sectionNamePattern = Pattern.compile("<a name='(.*?)' *></a>");

    public static void main(String[] args) throws IOException {
        // init

        BufferedReader bufferedReader;
        if (!isDebug) {
            bufferedReader = new BufferedReader(new InputStreamReader(System.in, "utf-8"));
        } else {
            bufferedReader = new BufferedReader(new FileReader("module-user.md"));
        }
        boolean isDirectory = false;
        List<Directory> directories = new ArrayList<>();
        List<String> contents = new ArrayList<>();
        Directory directory = null;
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            if (!isDirectory) {
                if (line.equals(directoryStart)) {
                    isDirectory = true;
                } else {
                    if (directory == null) {
                        Matcher matcher = sectionNamePattern.matcher(line);
                        if (matcher.find()) {
                            String name = trim(matcher.group(1));
                            directory = new Directory().setName(name);
                            contents.add(line);
                        } else {
                            contents.add(line);
                        }
                    } else {
                        Matcher matcher = sectionTitlePattern.matcher(line);
                        if (matcher.find()) {
                            String content = trim(matcher.group(3));
                            int level = matcher.group(2).length();
                            directory.setContent(content).setLevel(level);
                            directories.add(directory);

                            String fullContent = trim(matcher.group(1));
                            contents.add(fullContent);
                        } else {
                            contents.add(line);
                        }
                        directory = null;
                    }
                }
            } else {
                if (line.equals(directoryEnd)) {
                    isDirectory = false;
                } else {
                    // do nothing
                }
            }
        }

        printDirectory(directories);
        printContent(contents);
    }

    private static void printDirectory(List<Directory> directories) throws UnsupportedEncodingException {
        int minLevel = Integer.MAX_VALUE;
        for (Directory directory : directories) {
            if (directory.getLevel() < minLevel) {
                minLevel = directory.getLevel();
            }
        }

        print(directoryStart);
        print("# 目录");
        for (Directory directory : directories) {
            StringBuilder marks = new StringBuilder();
            marks.append("###");
            for (int i = minLevel; i < directory.getLevel(); i++) {
                marks.append("#");
            }
            marks.append(" ");
            for (int i = minLevel; i < directory.getLevel(); i++) {
                marks.append("&nbsp;&nbsp;&nbsp;&nbsp;");
            }
            print(String.format("%s[%s](#%s)  ", marks.toString(), directory.getContent(), directory.getName()));
        }
        print("");
        print("---");
        print("");
        print("---");
        print(directoryEnd);
    }

    private static void printContent(List<String> contents) throws UnsupportedEncodingException {
        for (String content : contents) {
            print(content);
        }
    }

    private static void print(String string) throws UnsupportedEncodingException {
        System.out.println(new String(string.getBytes("utf-8")));
    }

    private static String trim(String str) {
        return str.replace("&nbsp;", "").trim();
    }

    private static class Directory {
        private String name;
        private int level;
        private String content;

        public String getName() {
            return name;
        }

        public Directory setName(String name) {
            this.name = name;
            return this;
        }

        public int getLevel() {
            return level;
        }

        public Directory setLevel(int level) {
            this.level = level;
            return this;
        }

        public String getContent() {
            return content;
        }

        public Directory setContent(String content) {
            this.content = content;
            return this;
        }
    }
}
